﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LeetCodeSolutions
{
    /// <summary>
    /// 532. 数组中的 k-diff 数对
    /// </summary>
    class LeetCode532: LeetCodeBase
    {
        public override void Execute()
        {
            CommonFuntions.Print(FindPairs
                (
                new int[] { 1, 3, 1, 5, 4 },
                0
                ));
        }

        public int FindPairs(int[] nums, int k)
        {
            List<int> diffNumsList = new List<int>();
            for (int i = 0; i < nums.Length; i++)
            {
                if (!diffNumsList.Contains(nums[i]))
                {
                    diffNumsList.Add(nums[i]);
                }
            }

            List<Pairs> pairsList = new List<Pairs>();
            int kDiffCount = 0;
            for (int i = 0; i < diffNumsList.Count; i++)
            {
                for (int j = 0; j < diffNumsList.Count; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }

                    Pairs p = new Pairs(diffNumsList[i], diffNumsList[j]);
                    if (pairsList.Exists((pair) => {
                        return pair.Same(diffNumsList[i], diffNumsList[j]);
                        }))
                    {
                        continue;
                    }

                    if (Math.Abs(diffNumsList[i] - diffNumsList[j]) == k)
                    {
                        kDiffCount++;
                        pairsList.Add(p);

                        CommonFuntions.Print(diffNumsList[i] + " " + diffNumsList[j]);
                    }
                }
            }

            return kDiffCount;
        }

        public class Pairs
        {
            int x, y;

            public Pairs(int x, int y)
            {
                if (x <= y)
                {
                    this.x = x;
                    this.y = y;
                }
                else
                {
                    this.x = y;
                    this.y = x;
                }
            }

            public bool Same(int i, int j)
            {
                if ((x == i && y == j) || (x == j && y == i))
                {
                    return true;
                }

                return false;
            }
        }
    }
}
